package com.xianbeiying.app.ckgl.downfile;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.activity.BaseActivity;
import com.xianbeiying.app.ckgl.activity.MainActivity;

/**
* @Title: NotificationUpdateActivity.java 
* @Package com.cn.sh.issaver.elephantshop.app 
* @Description:下载更新界面
* @author Yj   
* @date 2015年3月18日 下午2:50:20 
* @version V1.0   
*/
public class NotificationUpdateActivity extends BaseActivity {
	private Button btn_cancel,btn_background;// btn_update,
	private TextView tv_progress;
	private DownloadService.DownloadBinder binder;
	private boolean isBinded;
	private ProgressBar mProgressBar;
	private String downloadUrl;
	//
	private boolean isDestroy = true;
	private MyApplication app;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update);
		app = (MyApplication) getApplication();
		// btn_update = (Button) findViewById(R.id.update);
		btn_cancel = (Button) findViewById(R.id.cancel);
		btn_background = (Button) findViewById(R.id.background);
		tv_progress = (TextView) findViewById(R.id.currentPos);
		mProgressBar = (ProgressBar) findViewById(R.id.progressbar1);
		btn_cancel.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				// TODO Auto-generated method stub
				MainActivity.isAddFragment=false;
				binder.cancel();
				binder.cancelNotification();
				//取消跳转；标志
				finish();
			}
		});
		btn_background.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MainActivity.isAddFragment=false;
				finish();
			}
		});
	}
	ServiceConnection conn = new ServiceConnection(){
		@Override
		public void onServiceDisconnected(ComponentName name){
			// TODO Auto-generated method stub
			isBinded = false;
		}
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			binder = (DownloadService.DownloadBinder) service;
			// ��ʼ����
			isBinded = true;
			binder.addCallback(callback);
			binder.start();

		}
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (isDestroy && app.isDownload()) {
			Intent it = new Intent(NotificationUpdateActivity.this, DownloadService.class);
			startService(it);
			bindService(it, conn, Context.BIND_AUTO_CREATE);
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		if (isDestroy && app.isDownload()) {
			Intent it = new Intent(NotificationUpdateActivity.this, DownloadService.class);
			startService(it);
			bindService(it, conn, Context.BIND_AUTO_CREATE);
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		isDestroy = false;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		MainActivity.isAddFragment=false;
		if (isBinded) {
			unbindService(conn);
		}
		if (binder!=null &&binder.isCanceled()) {
			Intent it = new Intent(this, DownloadService.class);
			stopService(it);
		}
	}

	private ICallbackResult callback = new ICallbackResult() {

		@Override
		public void OnBackResult(Object result) {
			// TODO Auto-generated method stub
			if ("finish".equals(result)) {
				MainActivity.isAddFragment=false;
				finish();
				return;
			}
			int i = (Integer) result;
			mProgressBar.setProgress(i);
			
			mHandler.sendEmptyMessage(i);
		}

	};

	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			tv_progress.setText("当前进度" + msg.what + "%");

		};
	};

	public interface ICallbackResult {
		public void OnBackResult(Object result);
	}

//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			//写下你希望按下返回键达到的效果代码，不写则不会有反应
//			return false;
//		}
//		return super.onKeyDown(keyCode, event);
//	}
}
